/*
 * ClockCore.java
 */

/* Copyright information:
 * 
 *  Copyright 2010 Failboat Productions. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 * 
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY FAILBOAT PRODUCTIONS ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL FAILBOAT PRODUCTIONS OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of Failboat Productions.
 */

package GUI;

import java.io.Serializable;

/**
 * Class that handles the timers used in ChessGUI
 * Serialized to be saved with the game
 * @author Failboat
 *
 */


class ClockCore implements Serializable
{
	private static final long serialVersionUID = -6141568614879860413L;
	private int seconds =0;
	private int minutes =0;
	private int turns = 0;
    private boolean countdown = false;
    private boolean quick = false;
    
     
    /* Constructor */
	ClockCore()
	{
    if(countdown && !quick){
    	minutes = 90;
    	}else if(countdown && quick){
    		minutes = 10;
    	}else{
    		minutes = 0;
    	}
    seconds = 0;
    }
    
	/**
	 * Start or stop the timer, handles turnChanged if countdown enabled
	 * @param start
	 * (boolean)
	 * @param turnChanged
	 * (boolean)
	 */
  void runTimer(boolean start, boolean turnChanged){ //turned has not changed if turnChanged is false
       
    	/* Increase turn by one if turn has changed */
    	if(!start && turnChanged && countdown && !quick){
        	turns++;
        	if(turns>0){
        		seconds+=30;
        		if(seconds>59){
        			minutes += seconds/60;
        			seconds = seconds%60;
        		}
        	}
        	
        	/* At 40 turns, take whats left of added seconds and set minutes to 30 */
        	if(turns == 40){
        		if(((minutes * 60) + seconds)<1200){
        			seconds = (minutes * 60) + seconds;
        			minutes = 30;
        		}else{
        			minutes = 50;
        			seconds = 0;
        		}
        		fixTime();
        	}
        }
    }
    
  /**
   * Method that resets everything
   */
    void resetTimer(){
    	if(countdown&&!quick){
    		minutes =90;
    	}else if(countdown&&quick){
    		minutes = 10;
    	}else{
    		minutes =0;
    	}
    	
    	seconds =0;
    	turns =0;
    }
    
    /**
     * Method that is used by runTimer to add time every second
     */
    void addSecond(){

    	if(countdown){
    		seconds--;
    	}else{
    		seconds++;
    	}
    	fixTime();
    }
    
    /**
     * Method that handles minutes and seconds
     */
    private void fixTime(){
    	if(seconds>59){
    		minutes += seconds/60;
    		seconds = seconds%60;
    	}
    	if(seconds<0){
    		minutes --;
    		seconds += 60;
    	}
    }
    
    /**
     * Method that gives a string to put in the clock from minutes and seconds
     * @return clockString
     * (String)
     */
    String clockString(){
    	String time = null;
    	if(minutes*60+seconds<0){
    		time = "00:00";
    	}else{
	    	if(minutes<10){
	    		time=("0"+minutes);
	    	}else{
	    		time=""+minutes;
	    	}
	    	if(seconds<10){
	    		time+=(":0"+seconds);
	    	}else{
	    		time+=":"+seconds;
	    	}
    	}
    	return time;
    }
    /**
     * Method that returns if countdown is used
     * @return countdown
     * (boolean)
     */
    boolean isCountdown(){
    	return countdown;
    }
    
    boolean isQuick(){
    	return quick;
    }
    
    /**
     * Returns remaining time in seconds
     * @return (int) time in seconds
     * 
     */
    int getTime(){
    	return (minutes*60)+seconds;
    }
    
    /**
     * Method to enable countdown
     * @param countdown
     */
    void setCountdown(boolean countdown, boolean quick){
    	this.countdown = countdown;
    	this.quick = quick;
    }
}

